<template>
  <div style="width:888px;">
    <el-card class="box-card">
      <el-upload
        class="avatar-uploader"
        :action="upload"
        :data="{userId: user_id}"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
        style="text-align:center"
      >
        <img v-if="user_avatar" :src="user_avatar" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon" />
      </el-upload>
    </el-card>
    <el-card class="box-card">
      <div slot="header" class="clearfix" style="font-size:17px;font-family:Arial">
        <strong>账号设置</strong>
      </div>
      <div class="text-box">
        <el-row style="">
          <el-col :span="2">
            <span style="font-size:14px">密码</span>
          </el-col>
          <el-col :span="20">
            <img>
          </el-col>
          <el-col :span="2">
            <el-link :underline="false" @click="open('pass')"><span style="color:#409EFF">修改密码</span></el-link>
          </el-col>
        </el-row>
        <el-divider />
        <el-row style="">
          <el-col :span="2">
            <span style="font-size:14px">手机</span>
          </el-col>
          <el-col :span="18">
            <img>
          </el-col>
          <el-col :span="2">
            <span v-if="user_phone === null" style="color:red;font-size:13px"> 未填写 </span>
            <img v-else>
          </el-col>
          <el-col :span="2">
            <el-link :underline="false" @click="open('phone')">
              <span style="color:#409EFF">修改手机</span>
            </el-link>
          </el-col>
        </el-row>
        <el-divider />
        <el-row style="">
          <el-col :span="2">
            <span style="font-size:14px">邮箱</span>
          </el-col>
          <el-col :span="18">
            <img>
          </el-col>
          <el-col :span="2">
            <span v-if="user_email === null" style="color:red;font-size:13px"> 未填写 </span>
            <img v-else>
          </el-col>
          <el-col :span="2">
            <el-link :underline="false" @click="open('email')"><span style="color:#409EFF">修改邮箱</span></el-link>
          </el-col>
        </el-row>
        <el-divider />
        <el-row style="">
          <el-col :span="2">
            <span style="font-size:14px">账号注销</span>
          </el-col>
          <el-col :span="20">
            <img>
          </el-col>
          <el-col :span="2">
            <el-link :underline="false" @click="cancellation"><span style="color:#409EFF">立即注销</span></el-link>
          </el-col>
        </el-row>
        <el-divider />
      </div>
    </el-card>
    <!-- 修改密码弹框 -->
    <el-dialog title="修改密码" :visible.sync="passwordVisible">
      <el-form :model="passform">
        <el-form-item label="新密码" :label-width="formLabelWidth">
          <el-input v-model="passform.password" type="password" clearable show-password autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="passwordVisible = false">取 消</el-button>
        <el-button type="primary" @click="password">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 修改手机弹框 -->
    <el-dialog title="修改手机" :visible.sync="phoneVisible">
      <el-form>
        <el-form-item v-if="user_phone !== null" label="旧手机号" :label-width="formLabelWidth">
          <el-input :value="user_phone" readonly autocomplete="off" />
        </el-form-item>
        <el-form-item label="新手机号" :label-width="formLabelWidth">
          <el-input v-model="phone" clearable autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="phoneVisible = false">取 消</el-button>
        <el-button type="primary" @click="phoneChange">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 修改邮箱弹框 -->
    <el-dialog title="修改邮箱" :visible.sync="emailVisible">
      <el-form>
        <el-form-item v-if="user_email !== null" label="旧手机号" :label-width="formLabelWidth">
          <el-input :value="user_email" readonly autocomplete="off" />
        </el-form-item>
        <el-form-item label="新邮箱号" :label-width="formLabelWidth">
          <el-input v-model="email" clearable autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="emailVisible = false">取 消</el-button>
        <el-button type="primary" @click="emailChange">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { edit, cancellation } from '@/api/user'
import { mapGetters } from 'vuex'
import Swal from 'sweetalert2'
export default {
  data() {
    return {
      formLabelWidth: '120px',
      passwordVisible: false,
      phoneVisible: false,
      emailVisible: false,
      email: '',
      phone: '',
      passform: {
        password: ''
      },
      upload: 'http://localhost:8081/test/user/avator'
    }
  },
  computed: {
    ...mapGetters([
      'user_avatar',
      'user_id',
      'user_phone',
      'user_email'
    ])
  },
  mounted() {
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.$store.dispatch('user/getInfo')
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 5

      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 5MB!')
      }
      return isLt2M
    },
    open(value) {
      if (value === 'pass') {
        this.passwordVisible = true
      } else if (value === 'phone') {
        this.phoneVisible = true
      } else if (value === 'email') {
        this.emailVisible = true
      }
    },
    password() {
      if (this.passform.password === '') {
        this.$message.error('不能为空')
        return
      }
      edit({ id: this.$store.state.user.id, password: this.passform.password }).then((res) => {
        if (res.code === 1) {
          this.$message({
            message: '修改密码成功',
            type: 'success'
          })
          this.passform.password = ''
          this.passwordVisible = false
          this.$store.dispatch('user/getInfo')
        } else {
          this.$message.error('修改密码失败')
        }
        this.passwordVisible = false
      })
    },
    phoneChange() {
      if (this.phone === '') {
        this.$message.error('不能为空')
        return
      }
      edit({ id: this.$store.state.user.id, phone: this.phone }).then((res) => {
        if (res.code === 1) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.phone = ''
          this.phoneVisible = false
          this.$store.dispatch('user/getInfo')
        } else {
          this.$message.error('修改失败')
        }
        this.passwordVisible = false
      })
    },
    emailChange() {
      if (this.email === '') {
        this.$message.error('不能为空')
        return
      }
      edit({ id: this.$store.state.user.id, email: this.email }).then((res) => {
        if (res.code === 1) {
          this.$message({
            message: '修改邮箱成功',
            type: 'success'
          })
          this.email = ''
          this.emailVisible = false
          this.$store.dispatch('user/getInfo')
        } else {
          this.$message.error('修改邮箱失败')
        }
        this.passwordVisible = false
      })
    },
    cancellation() {
      Swal.fire({
        title: '确定要注销吗?',
        icon: 'question',
        confirmButtonText: '确定',
        cancelButtonText: '让我再想想',
        showCancelButton: true,
        heightAuto: false
      }).then(res => {
        if (res.isConfirmed) {
          cancellation({ userId: this.user_id }).then(res => {
            Swal.fire({
              title: '注销成功',
              icon: 'success',
              confirmButtonText: '确定',
              heightAuto: false
            })
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload {
    border: 1px dashed #b49090;
    border-radius: 100px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    border-radius: 100px;
  }
  .avatar :hover {
    filter:alpha(Opacity=80);
    -moz-opacity:0.8;
    opacity: 0.8;
  }
</style>
